<?php
require_once PATH_SITE.'cn/royan/php/utils/Logger.util.php';
require_once PATH_SITE.'cn/royan/php/parses/ConnectParse.parse.php';
require_once PATH_SITE."cn/royan/php/utils/builders/BuilderEvent.builder.php";
require_once PATH_SITE."cn/royan/php/utils/writers/WriterRoomList.writer.php";
require_once PATH_SITE.'cn/royan/php/utils/Plugins.util.php';
require_once PATH_SITE.'cn/royan/php/proxys/SocketProxy.proxy.php';
require_once PATH_SITE.'cn/royan/php/parses/SimpleParse.class.php';
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserLoginParse
 *
 * @author yanr
 */
class UserLoginParse extends SimpleParse
{
    //put your code here
    const TYPE          = "UserLogin";
    const ERROR_10001   = "error_10001";//has login
    const ERROR_10002   = "error_10002";//login failure
    
//    static public function getInstance()
//    {
//        IF( is_null( self::$instance ) ) self::$instance = new UserLoginParse( UserLoginParse::TYPE );
//        RETURN self::$instance;
//    }
    
    public function __construct($typeName = NULL) {
        parent::__construct( UserLoginParse::TYPE );
        Logger::trace( "UserLoginParse Open" );
    }
    
    public function parseInfo($info)
    {
        IF( parent::parseInfo($info) === FALSE ) RETURN;
        
        $feedback = TRUE;
        $pluginClass = Plugins::findPlugin( UserLoginParse::TYPE );

        IF ( $pluginClass !== FALSE )
        {
            $feedback = FALSE;
            $plugin = new $pluginClass( $info["data"]->username, $info["data"]->password, $info["user"] );
            $feedback = $plugin->excute();
        }
        
        IF ( $feedback === TRUE )
        {
            Logger::trace( "User Login Success" );
            $socketProxy = $this->facade->retrieveProxy( SocketProxy::NAME );
            $socket = $socketProxy->getData();
            $area = $socket->__get("default");
            
            $info["user"]->__set("isLogin", TRUE);
            $info["user"]->__set("area",    $area);
            
            $writerRoomList = new WriterRoomList( $area );
            
            $data = array( "event"=>    BuilderEvent::SUCCESS,
                           "target"=>   UserLoginParse::TYPE,
                           "user"=>     $info["user"],
                           "data"=>     $writerRoomList->write() );

            $this->sendNotification( ApplicationFacade::DISPATCH_EVENT, $data );
            
            $data = array( "user"=> $info["user"],
                           "area"=> $area,
                           "room"=> $area->__get("default"),
                           "type"=> UserEnterRoomParse::TYPE );
            
            $connectProxy = ConnectParse::getInstance();
            $connectProxy->parseInfo( $data );
        }
        ELSE
        {
            $data = array( "event"=>    BuilderEvent::ERROR,
                           "code"=>     UserLoginParse::ERROR_10002,
                           "target"=>   UserLoginParse::TYPE,
                           "user"=>     $info["user"]); 
            
            $this->sendNotification( ApplicationFacade::DISPATCH_EVENT, $data );
        }
    }
    
    protected function isValid($info)
    {
        IF ( parent::isValid($info) === FALSE ) RETURN FALSE;
        
        IF ( $info["user"]->__get("isLogin") === TRUE )
        {
            $data = array( "event"=>    BuilderEvent::ERROR,
                           "code"=>     UserLoginParse::ERROR_10001,
                           "target"=>   UserLoginParse::TYPE,
                           "user"=>     $info["user"]); 
            
            $this->sendNotification( ApplicationFacade::DISPATCH_EVENT, $data );
            RETURN FALSE;
        }
        RETURN TRUE;
    }
}
?>